package cn.stylefeng.guns.modular.rest;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.util.DateUtils;
import cn.stylefeng.guns.modular.ht.entity.Report;
import cn.stylefeng.guns.modular.ht.model.params.BackRegisterParam;
import cn.stylefeng.guns.modular.ht.model.params.ReportParam;
import cn.stylefeng.guns.modular.ht.model.result.ReportResult;
import cn.stylefeng.guns.modular.ht.service.BackRegisterService;
import cn.stylefeng.guns.modular.ht.service.ReportService;
import cn.stylefeng.guns.modular.school.entity.TeacherInfo;
import cn.stylefeng.guns.modular.school.entity.TripInfo;
import cn.stylefeng.guns.modular.school.entity.TripOrderInfo;
import cn.stylefeng.guns.modular.school.model.result.TripInfoResult;
import cn.stylefeng.guns.modular.school.service.TeacherInfoService;
import cn.stylefeng.guns.modular.school.service.TripInfoService;
import cn.stylefeng.guns.modular.school.service.TripOrderInfoService;
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/trip")
public class ApiNeedTokenTripController extends BaseController {
    @Autowired
    private TripInfoService tripInfoService;
    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private TripOrderInfoService tripOrderInfoService;

    /**
     * 绑定
     */
    @RequestMapping(value = "list")
    public Object list(String username) {
        Map map = new HashMap();
        String today = DateUtil.today();
        // 判断身份
        TeacherInfo teacherInfo = teacherInfoService.getOne(new QueryWrapper<TeacherInfo>().eq("CODE", username));
        if (teacherInfo == null) {
            map.put("role", "student");
            map.put("end", FastDateFormat.getInstance("yyyy-MM-dd").format(DateUtil.offsetDay(new DateTime(), 2)));
        } else {
            map.put("end", FastDateFormat.getInstance("yyyy-MM-dd").format(DateUtil.offsetDay(new DateTime(), 3)));
            map.put("role", "teacher");
        }


        List<TripInfo> tripInfoList = tripInfoService.list();
        List<TripInfoResult> tripInfoResultList = new ArrayList<>();
        tripInfoList.forEach(tripInfo -> {
            TripInfoResult tripInfoResult = new TripInfoResult();
            tripInfoResultList.add(tripInfoResult);
            ToolUtil.copyProperties(tripInfo, tripInfoResult);
            //已预约座位
            int count = tripOrderInfoService.count(new QueryWrapper<TripOrderInfo>().eq("trip_id", tripInfo.getId()).eq("date_format(order_date,'%Y-%m-%d')", today));
            // 休息日判断
            if (DateUtil.dayOfWeek(new DateTime()) == 7 || DateUtil.dayOfWeek(new DateTime()) == 1) {
                tripInfoResult.setSeatNum(tripInfo.getHolidayNum() - count);
            } else {
                tripInfoResult.setSeatNum(tripInfo.getWorkdayNum() - count);
            }
        });

        map.put("today", today);
        map.put("now", FastDateFormat.getInstance("HH:mm").format(new DateTime()));
        map.put("tripInfoList", tripInfoResultList);


        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }


}


